package vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import test.PerformanceAnalyze;

import java.util.Arrays;

public class TCPServer extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

        NetServerOptions options = new NetServerOptions();
        options.setTcpNoDelay(false);

        NetServer server = vertx.createNetServer(options);

        server.connectHandler(socket -> {
            // Handle the connection in here
            socket.handler(buffer -> {
                Arrays.stream(buffer.toString().split(",")).forEach(time -> {
                    try {
                        Long nanoTime = Long.parseLong(time);
                        performanceAnalyze.delay(System.nanoTime()-nanoTime);
                        //socket.write(Buffer.buffer(nanoTime +","));
                    } catch (Exception e) {

                    }
                    performanceAnalyze.meter();
                });
            });
        });

        server.listen(1234, "0.0.0.0", res -> {
            if (res.succeeded()) {
                System.out.println("Server is now listening!");
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }

    public static void main(String[] args) {

    }
}
